<template>
  <div class="goods-list">
  <!-- 顶部组件 -->
   <My-Header header_title="商品列表"></My-Header>
  <!-- 商品列表 -->
    <ul>
      <!-- tag属性：具有tag属性的router-link会被渲染成相应的标签  -->
      <router-link class="goods-li" :to="'/goodsDetail/'+item.id" tag="li" v-for="item in goodsList" :key="item.id">
        <div class="goods-container">
          <img :src="item.img_url" :alt="item.title">
          <h4>{{item.title}}</h4>
          <div class="goods-info">
            <div class="price-info"><span class="price-new">￥{{item.sell_price}}</span><span class="price-old">￥{{item.market_price}}</span></div>
            <div class="sell-info">
              <span class="sell-status">预览：{{item.click}}次</span>
              <span class="sell-leftcount">库存：{{item.stock_quantity}}</span>
            </div>
          </div>
        </div>
      </router-link>
    </ul>
  <!-- 加载更多  -->
     <div class="button-group">
      <mt-button size="large" type="primary" @click="loadMore" v-if="moreButton">加载更多</mt-button>
      <mt-button size="large" type="danger" disabled v-else>没有更多了</mt-button>
    </div>
  </div>
</template>

<script>
// 引入已封装好MyHeader组件
import MyHeader from '@/components/MyHeader'
// 引入封装好的请求函数
import { getGoodsList } from '@/api'

export default {
  // 注入需要使用的组件
  components: {
    // 头部组件
    MyHeader
  },
  data () {
    return {
      // 获取商品列表页码
      pageindex: 1,
      // 商品列表
      goodsList: [],
      // 加载更多按钮的显示状态
      moreButton: true
    }
  },
  created () {
    /* 初始化商品列表 */
    this.initGoodsList()
  },
  methods: {
    /* 初始化商品列表 */
    initGoodsList () {
      getGoodsList(this.pageindex)
        .then(res => {
          if (res.status === 0) {
            this.goodsList = res.message
          }
        })
    },
    /* 加载更多 */
    loadMore () {
      // 先储存已获取的商品列表
      let oldGoodsList = this.goodsList
      // 新页码列表数据
      this.pageindex++
      // 发请求获取新一页列表数据
      getGoodsList(this.pageindex)
        .then(res => {
          if (res.status === 0) {
            // 新页码列表
            let newGoodsList = res.message
            // 追加数据
            this.goodsList = [...oldGoodsList, ...newGoodsList]
            // 若到达最后一页 => 显示没更多
            if (res.message.length < 10) {
              this.moreButton = false
            }
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-list {
  margin: 40px 0 0 0;
  background-color: #f4f4f4;
  ul {
    display: flex;
    flex-wrap: wrap;
    font-size: 10px;
  }
  h4 {
    font-size: 12px;
    font-weight: bold;
    height: 32px;
    // 多行文本超出省略号
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .goods-li {
    width: 50%;
    box-sizing: border-box;
    padding-top: 5px;
    display: flex;
    justify-content: column;
    .goods-container {
      width: 100%;
      background-color: #fff;
      padding: 5px;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .price-info {
      display: flex;
      justify-content: space-between;
      .price-new {
        color: red;
      }
      .price-old {
        text-decoration: line-through;
      }
    }
    .sell-info {
      display: flex;
      justify-content: space-between;
    }
  }

  .goods-li:nth-child(2n+1) {
    padding-right: 5px;
  }
  .goods-li:nth-child(2n) {
    padding-left: 5px;
  }
}
</style>
